<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <title>后台管理</title>
    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://unpkg.com/bootstrap-table@1.15.5/dist/bootstrap-table.min.css">
    <!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
    <script src="js/jquery-3.4.1.min.js"></script>
    <!-- 加载jsRender-->
    <script src="js/jsrender.js" type="text/javascript" charset="utf-8"></script>
    <script src="js/jsrender.min.js" type="text/javascript" charset="utf-8"></script>
    <!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
    <script src="js/bootstrap.min.js" type="text/javascript" charset="utf-8"></script>
    <!-- Bootstrap-table -->
    <script src="js/bootstrap-table.min.js"></script>
    <script src="js/bootstrap-table-zh-CN.min.js"></script>
    <style>
        body {
            background-color: #eee;
        }

        .row {
            padding: 20px;
            margin-bottom: 10px;
        }

        .panel-default>.panel-heading, .panel-warning>.panel-heading,
        .panel-danger>.panel-heading, .panel-info>.panel-heading {
            background-color: white;
            font-weight: bold;
        }

        .row>.panel {
            background-color: white;
            box-shadow: 1px 1px 3px #0f0f0f, -1px -1px 3px #0f0f0f;
        }

        .row {
            background-color: rgba(255, 255, 255, .5);
        }

        .panel-default>.count-top {
            font-size: 14px;
            color: rgba(77, 77, 77, .5);
        }

        .panel-default>.count {
            font-size: 30px;
            margin-left: 20px;
            font-weight: bolder;
            text-align: center;
        }
        .input-group-addon {
            width: 80px;
            font-weight: bold;
            padding: 5px;
        }
        .input-group input {
            padding-left: 10px;
        }
        .input-group .form-check{
            margin-left: 10px;
        }



    </style>
</head>
<body>
    <nav class="navbar navbar-default">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#">
                    <img alt="Brand" src="./images/M.svg" width="25" height="25">
                </a>
            </div>
            <div class="collapse navbar-collapse"  id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav">
                    <li><a href="./index.html">首页</a></li>
                    <li><a href="./user.html">用户管理</a></li>
                    <li class="active"><a href="#">产品管理</a></li>
                    <li><a href="./note.html">留言管理</a></li>
                </ul>
                <ul class="nav navbar-nav navbar-right">
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
                            <span class="glyphicon glyphicon-user" aria-hidden="true"></span>
                            <span id="show-uname" class="text-info" style="margin-left: 5px;">Admin</span>
                            <span class="caret"></span>
                        </a>
                        <ul class="dropdown-menu">
                            <li>
                                <a href="">
                                    <span class="glyphicon glyphicon-lock" aria-hidden="true"></span>
                                    <span class="text-info" style="margin-left: 5px;">修改密码</span>
                                </a>
                            </li>
                            <li>
                                <a href="#">
                                    <span class="glyphicon glyphicon-log-out" aria-hidden="true"></span>
                                    <span class="text-info" style="margin-left: 5px;">管理员退出</span>
                                </a>
                            </li>
                        </ul>
                    </li>
                </ul>
            </div>
        </div>
    </nav>
    <div class="container">
        <div class="row">
            <div class="col-md-12 panel panel-info well">
                <div class="panel-heading">
                    <span>产品管理</span>
                    <span>
                        <button id="addUser" class="btn btn-danger btn-sm">添加产品</button>
                    </span>
                </div>
                <table id="table" class="table"></table>
            </div>
        </div>
    </div>
    <div id="body"></div>
    <!-- Modal -->
    <div class="modal fade" id="staticBackdrop" data-backdrop="static" tabindex="-1" role="dialog" aria-labelledby="staticBackdropLabel" aria-hidden="true">
        <div class="modal-dialog modal-dialog-scrollable" role="document">
            <div class="modal-content">
                <form id="update-form">
                    <div class="modal-header">
                        <h5 class="modal-title" id="staticBackdropLabel">修改产品数据</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body" id="modal-body">

                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
                        <button id="btn-submit" type="button" class="btn btn-primary">提交修改</button>
                    </div>
                </form>

            </div>
        </div>
    </div>
    <!-- Modal2   -->
    <div class="modal fade" id="staticBackdrop1" data-backdrop="static" tabindex="-1" role="dialog" aria-labelledby="staticBackdropLabel" aria-hidden="true">
        <div class="modal-dialog modal-dialog-scrollable" role="document">
            <div class="modal-content">
                <form id="add-form">
                    <div class="modal-header">
                        <h5 class="modal-title" id="staticBackdropLabel1">添加新产品</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body" id="modal-body1">
                        <ul class="list-group">
                            <li class="list-group-item">
                                <div class="input-group">
                                    <span class="input-group-addon">产品id</span>
                                    <input type="text" aria-readonly="true" readonly class="form-control-plaintext" id="staticEmail" value="由系统自动生成,无法自定义">
                                </div>
                            </li>
                            <li class="list-group-item">
                                <div class="input-group">
                                    <span class="input-group-addon">产品名</span>
                                    <input type="text" name="pname" class="form-control" id="pname" value="">
                                </div>
                            </li>
                            <li class="list-group-item">
                                <div class="input-group">
                                    <span class="input-group-addon">价格</span>
                                    <input type="text" name="price" class="form-control" id="price" value="">
                                </div>
                            </li>
                            <li class="list-group-item">
                                <div class="input-group">
                                    <span class="input-group-addon">类别</span>
                                    <input type="text" name="sort" class="form-control" id="sort" value="">
                                </div>
                            </li>
                            <li class="list-group-item">
                                <div class="input-group">
                                    <span class="input-group-addon">数量</span>
                                    <input type="text" name="count" class="form-control" id="count" value="">
                                </div>
                            </li>
                            <li class="list-group-item">
                                <div class="input-group">
                                    <span class="input-group-addon">销量</span>
                                    <input type="text" name="sales" class="form-control" id="sales" value="">
                                </div>
                            </li>
                            <li class="list-group-item">
                                <div class="input-group">
                                    <span class="input-group-addon">所属用户</span>
                                    <input type="text" name="uid" class="form-control" id="uid" value="">
                                </div>
                            </li>
                            <li class="list-group-item">
                                <div class="input-group">
                                    <span class="input-group-addon">描述</span>
                                    <input type="text" name="info" class="form-control" id="info" value="">
                                </div>
                            </li>
                        </ul>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
                        <button id="add-submit" type="button" class="btn btn-primary">确定添加</button>
                    </div>
                </form>

            </div>
        </div>
    </div>
    <!-- template-->
    <script id="modalbody" type="text/x-jsrender">
            <ul class="list-group">
                <li class="list-group-item">
                    <div class="input-group">
                        <span class="input-group-addon">Pid</span>
                        <input type="text" name="pid" readonly class="form-control-plaintext" id="staticEmail" value="{{:pid}}">
                    </div>
                </li>
                <li class="list-group-item">
                    <div class="input-group">
                        <span class="input-group-addon">产品名</span>
                        <input type="text" name="pname" class="form-control" id="pname" value="{{:pname}}">
                    </div>
                </li>
                <li class="list-group-item">
                    <div class="input-group">
                        <span class="input-group-addon">价格</span>
                        <input type="text" name="price" class="form-control" id="price" value="{{:price}}">
                    </div>
                </li>
                <li class="list-group-item">
                    <div class="input-group">
                        <span class="input-group-addon">类别</span>
                        <input type="text" name="sort" class="form-control" id="sort" value="{{:sort}}">
                    </div>
                </li>
                <li class="list-group-item">
                    <div class="input-group">
                        <span class="input-group-addon">数量</span>
                        <input type="text" name="count" class="form-control" id="count" value="{{:count}}">
                    </div>
                </li>
                <li class="list-group-item">
                    <div class="input-group">
                        <span class="input-group-addon">销量</span>
                        <input type="text" name="sales" class="form-control" id="sales" value="{{:sales}}">
                    </div>
                </li>
                <li class="list-group-item">
                    <div class="input-group">
                        <span class="input-group-addon">发表用户</span>
                        <input type="text" name="uid" class="form-control" id="uid" value="{{:uid}}">
                    </div>
                </li>
                <li class="list-group-item">
                    <div class="input-group">
                        <span class="input-group-addon">产品描述</span>
                        <input type="text" name="info" class="form-control" id="info" value="{{:info}}">
                    </div>
                </li>
            </ul>
    </script>
    <script id="modalbody2" type="text/x-jsrender">
        <ul class="list-group">
            <li class="list-group-item">
                <span class="badge">{{:pid}}</span>
                产品id
            </li>
            <li class="list-group-item">
                <span class="badge">{{:pname}}</span>
                产品名称
            </li>
            <li class="list-group-item">
                <span class="badge">{{:price}}</span>
                产品价格
            </li>
            <li class="list-group-item">
                <span class="badge">{{:sort}}</span>
                类别
            </li>
            <li class="list-group-item">
                <span class="badge">{{:count}}</span>
                数量
            </li>
            <li class="list-group-item">
                <span class="badge">{{:sales}}</span>
                销量
            </li>
            <li class="list-group-item">
                <span class="badge">{{:uid}}</span>
                所属用户uid
            </li>
            <li class="list-group-item">
                <span class="badge">{{:info}}</span>
                产品描述
            </li>
        </ul>
    </script>
    <script>
        $("#addUser").click(function () {
            $("#staticBackdrop1").modal("show");
        });
        $("#add-submit").click(function () {
            $.ajax({
                type: "POST",   //提交的方法
                url:"http://localhost:8080/ycumall/api/admin.do?method=addProduct", //提交的地址
                data:$("#add-form").serialize(),// 序列化表单值
                async: false,
                dataType: "json",
                error: function(XMLHttpRequest, textStatus, errorThrown) {  //失败的话
                    alert("连接错误!");
                    // 状态码
                    console.log(XMLHttpRequest.status);
                    // 状态
                    console.log(XMLHttpRequest.readyState);
                    // 错误信息
                    console.log(textStatus);
                },
                success: function(data) {//成功
                    if(data.status === 200) {
                        alert("添加成功!")
                    } else {
                        alert("添加失败!");
                    }
                }
            });
            $("#staticBackdrop1").modal('hide');
            $("#table").bootstrapTable("refresh");
        });
        $.get("http://localhost:8080/ycumall/api/admin.do?method=getUserInfo", function (data) {
            $("#show-uname").html(data.data.username);
            console.log(data.data.username);
        });
        $("#btn-submit").click(function () {
            $.ajax({
                type: "POST",   //提交的方法
                url:"http://localhost:8080/ycumall/api/admin.do?method=updateProduct", //提交的地址
                data:$('#update-form').serialize(),// 序列化表单值
                async: false,
                dataType: "json",
                error: function(XMLHttpRequest, textStatus, errorThrown) {  //失败的话
                    alert("连接错误!");
                    // 状态码
                    console.log(XMLHttpRequest.status);
                    // 状态
                    console.log(XMLHttpRequest.readyState);
                    // 错误信息
                    console.log(textStatus);
                },
                success: function(data) {  //成功
                    if(data.status === 200) {
                        alert("修改成功!")
                        location.reload();
                    } else {
                        alert("修改失败!");
                    }
                }
            });
            $("#staticBackdrop").modal('hide');
        });
        function addFunctionAlty(value, row, index) {
            return [
                '<button id="delete" type="button" class="btn btn-primary"><span class="glyphicon glyphicon-remove-circle" style="color: red;"></span></button>',
                '<button id="update" type="button" class="btn btn-primary" style="margin-left: 5px; margin-right: 5px;" data-toggle="modal" data-target="#exampleModalCenter"><span class="glyphicon glyphicon-pencil" style="color:#5bc0de;"></span></button>',
                '<button id="view" type="button" class="btn btn-primary"><span class="glyphicon glyphicon-cloud-download" style="color: #ec971f;"></span></button>'
            ].join('');
        }
        window.operateEvents = {
            'click #delete': function (e, value, row, index) {
                var r = confirm("确定删除该商品?");
                if (r === true) {
                    $("#table").bootstrapTable('removeByUniqueId', row.pid);
                    $.post("http://localhost:8080/ycumall/api/admin.do?method=deleteProduct", "pid="+row.pid, function (data) {
                        if (data.status === 200) {
                            alert("删除成功!");
                        }
                    });
                }
            }, 'click #update': function (e, value, row, index) {
                var template = $.templates("#modalbody");
                var htmlOutput = template.render(row);
                $("#modal-body").html(htmlOutput);
                $("#staticBackdrop").modal('show');
            }, 'click #view': function (e, value, row, index) {
                var template = $.templates("#modalbody2");
                var htmlOutput = template.render(row);
                $("#modal-body").html(htmlOutput);
                $("#staticBackdrop").modal('show');
            }
        };
        $('#table').bootstrapTable({
            url: 'http://localhost:8080/ycumall/api/admin.do?method=getProduct',
            pagination: true,
            pageSize: 6,
            striped: true,
            sortName: 'pid',
            uniqueId: 'pid',
            sortOrder: 'desc',
            columns: [{
                field: 'pid',
                title: '产品ID'
            }, {
                field: 'pname',
                title: '产品名'
            }, {
                field: 'price',
                title: '价格'
            }, {
                field: 'count',
                title: '数量'
            }, {
                field: 'sales',
                title: '销量'
            }, {
                field: 'uid',
                title: '发表用户'
            }, {
                field: 'operate',
                title: '操作',
                events: operateEvents,//给按钮注册事件
                formatter: addFunctionAlty//表格中增加按钮
            }]
        });
    </script>
</body>
</html>